﻿@inherits ProComponentBase

<div @ref="Ref">
    <SSimpleModal Value="_visible" ValueChanged="HandleVisibleChanged" Title="@(GetTitle())" Class="full-height masa-modal" HeaderClass="mr-4" OnCancel="CloseModal">
        <ChildContent>
            <MStepper Value="_step" Class="d-flex flex-column full-height masa-stepper">
                <MStepperItems Class="full-height">
                    <MStepperContent Step="1" Class="full-height pa-0">
                        <div class="grid-card">
                            @foreach (var (key, value) in GetEnumMap<JobTypes>())
                            {
                                <div class="item">
                                    <MHover>
                                        <MCard @attributes="context.Attrs" Outlined Class="@($"d-flex justify-center align-center rounded-6 mb-6 {(context.Hover ? "primary" : "")}")" Style="border: 4px dashed #E2E7F4" OnClick="()=> SelectJobType(value)">
                                        <SButton Class="h6" Plain OnClick="()=> SelectJobType(value)" Color="@(context.Hover ? "fill" : "regular")">@T(key)</SButton>
                                        </MCard>
                                    </MHover>
                                </div>
                            }
                        </div>
                    </MStepperContent>
                    <MStepperContent Step="2" Class="full-height pa-0">
                        <MForm Model=Model EnableValidation @ref="basicForm" Class="full-height">
                            <AutoHeight Overflow>
                                <AutoHeightContent>
                                    <DefaultTitle Class="mt-0 mb-6">@T("BasicInformation")</DefaultTitle>
                                    <MRow NoGutters>
                                        <MCol Md="6">
                                            <STextField @bind-Value="@Model.Name" Label="@T("JobName")" HideDetails="@("auto")" />
                                        </MCol>
                                        @if (_isAdd)
                                        {
                                            <MCol Md="6" Class="pl-6">
                                                <SUserAutoComplete @bind-Value="Model.OwnerId" FillBackground="false" Dense Outlined Label="@T("Owner")" ></SUserAutoComplete>
                                            </MCol>
                                        }
                                        else
                                        {
                                            <MCol Md="6">
                                                <SSelect Class="ml-6" @bind-Value="Model.JobType" Label="@T("JobType")" HideDetails="@("auto")" Readonly Disabled Dense Outlined Items="GetEnumMap<JobTypes>()" ItemText="kv => T(kv.Key)" ItemValue="kv=>kv.Value">
                                                </SSelect>
                                            </MCol>
                                        }
                                    </MRow>
                                    @if (!_isAdd)
                                    {
                                        <MRow NoGutters Class="mt-9">
                                            <MCol Md="6">
                                            <SUserAutoComplete @bind-Value="Model.OwnerId" FillBackground="false" Dense Outlined Label="@T("Owner")"></SUserAutoComplete>
                                            </MCol>
                                        </MRow>
                                    }
                                    <MRow NoGutters Class="mt-9">
                                        <MCol Md="6" Class="d-flex align-center justify-space-between">
                                            <MSwitch @bind-Value="Model.IsAlertException" Label="@T("IsAlertException")" Class="ml-3 pt-0 mt-0" TValue="bool" OnClick="HandleAlertException" Inset></MSwitch>
                                            <MButton Disabled="!Model.IsAlertException" Outlined Large Class="emphasis2--text" Style="border: 1px solid #E4E8F3 !important;" OnClick="HandleAlertException">
                                                @T("ViewAlrmRule")
                                                <MIcon Right>
                                                    mdi-chevron-right
                                                </MIcon>
                                            </MButton>
                                        </MCol>
                                    </MRow>

                                    <MRow NoGutters Class="mt-9">
                                        <MCol Md="6">
                                            <SSelect Value="Model.ScheduleType" ValueExpression="()=>Model.ScheduleType" TValue="ScheduleTypes" TItemValue="ScheduleTypes" TItem="KeyValuePair<string,ScheduleTypes>" ValueChanged="OnScheduleTypeChanged" Label="@T("ScheduleType")" HideDetails="@("auto")" SingleLine Attach="true" Dense Outlined Clearable Items="GetEnumMap<ScheduleTypes>()" ItemText="kv => T(kv.Key)" ItemValue="kv=>kv.Value">
                                            </SSelect>
                                            </MCol>
                                            @if (Model.ScheduleType == ScheduleTypes.Cron)
                                        {
                                            <MCol Md="6">
                                                <STextField Class="ml-6" Value="Model.CronExpression" TValue="string" ValueChanged="OnCronValueChange" HideDetails="@("auto")" Label="@T("CronExpression")" Outlined Dense AppendIcon="mdi-clock" OnAppendClick="OpenCronModal">
                                                </STextField>
                                                </MCol>
                                        }
                                    </MRow>
                                    @if (Model.ScheduleType == ScheduleTypes.Cron && !string.IsNullOrWhiteSpace(Model.CronExpression))
                                    {
                                        <div class="mt-4">
                                            <p class="emphasis2--text">
                                                @T("ExpectedNextRunTime"):
                                            </p>
                                            <pre class="masa body2 regular--text">
                                                @_nextRunTimeStr
                                                                        </pre>
                                        </div>
                                    }
                                    <MRow NoGutters Class="mt-9">
                                        <MCol>
                                            <STextField @bind-Value="Model.NotifyUrl" Label="@T("JobNotifyUrl")" HideDetails="@("auto")" Dense Outlined />
                                        </MCol>
                                    </MRow>
                                    <DefaultTitle>@T("RunStrategy")</DefaultTitle>
                                    <MRow NoGutters Class="mt-9">
                                        <MCol Md="6">
                                            <SSelect Value="Model.RoutingStrategy" ValueExpression="()=>Model.RoutingStrategy" ValueChanged="OnRoutingStrategyChanged" TValue="RoutingStrategyTypes" TItem="KeyValuePair<string, RoutingStrategyTypes>" TItemValue="RoutingStrategyTypes" Label="@T("RoutingStrategy")" HideDetails="@("auto")" Attach="true" Dense Outlined Clearable Items="GetEnumMap<RoutingStrategyTypes>()" ItemText="kv => T(kv.Key)" ItemValue="kv=>kv.Value">
                                            </SSelect>
                                            </MCol>
                                            @if (Model.RoutingStrategy == RoutingStrategyTypes.Specified)
                                        {
                                            <MCol Md="6">
                                                <SSelect Class="ml-6" @bind-Value="Model.SpecifiedWorkerHost" Label="Worker" HideDetails="@("auto")" Attach="true" Dense Outlined Clearable Items="_workerList" ItemText="worker => worker.GetServiceUrl()" ItemValue="worker=> worker.GetServiceUrl()">
                                                </SSelect>
                                                </MCol>
                                        }
                                    </MRow>
                                    <MRow NoGutters Class="mt-9">
                                        <MCol Md="6">
                                            <SSelect @bind-Value="Model.ScheduleExpiredStrategy" Label="@T("ScheduleExpiredStrategy")" HideDetails="@("auto")" Attach="true" Dense Outlined Clearable Items="GetEnumMap<ScheduleExpiredStrategyTypes>()" ItemText="kv => T(kv.Key)" ItemValue="kv=>kv.Value">
                                            </SSelect>
                                            </MCol>
                                            <MCol Md="6">
                                            <SSelect Class="ml-6" @bind-Value="Model.ScheduleBlockStrategy" Label="@T("ScheduleBlockStrategy")" HideDetails="@("auto")" Attach="true" Dense Outlined Clearable Items="GetEnumMap<ScheduleBlockStrategyTypes>()" ItemText="kv => T(kv.Key)" ItemValue="kv=>kv.Value">
                                            </SSelect>
                                            </MCol>
                                            </MRow>
                                            <MRow NoGutters Class="mt-9">
                                        <MCol Md="6">
                                            <SSelect @bind-Value="Model.RunTimeoutStrategy" Label="@T("RunTimeoutStrategy")" HideDetails="@("auto")" Attach="true" Dense Outlined Clearable Items="GetEnumMap<RunTimeoutStrategyTypes>()" ItemText="kv => T(kv.Key)" ItemValue="kv=>kv.Value">
                                            </SSelect>
                                            </MCol>
                                            <MCol Md="6">
                                            <STextField Class="ml-6" @bind-Value="Model.RunTimeoutSecond" Type="number" NumberProps="(p)=>{ p.HideControl = true; p.Min=0; }" Label="@T("RunTimeoutSecond")" HideDetails="@("auto")" Dense Outlined />
                                        </MCol>
                                    </MRow>
                                    <DefaultTitle>@T("FailedStrategy")</DefaultTitle>
                                    <MRow NoGutters Class="mt-9">
                                        <MCol Md="4">
                                            <MButtonGroup Value="Model.FailedStrategy.ToString()" Class="elevation-tab buttongroup-radius10" Group Tile Dense Mandatory Borderless>
                                                @foreach (var (key, value) in GetEnumMap<FailedStrategyTypes>())
                                                {
                                                    <SButton OnClick="async() => await SwitchFailedStrategyType(value)" Value="@value.ToString()" Ripple=false MinWidth=100>
                                                        @T(key)
                                                    </SButton>
                                                }
                                            </MButtonGroup>
                                        </MCol>
                                        @if (Model.FailedStrategy == FailedStrategyTypes.Auto)
                                        {
                                            <MCol Md="4">
                                                <STextField Class="ml-6" Type="number" NumberProps="(p)=>{ p.HideControl = true; p.Min = 1; }" @bind-Value="Model.FailedRetryInterval" Label="@T("FailedRetryInterval")" HideDetails="@("auto")" Dense Outlined />
                                            </MCol>
                                            <MCol Md="4">
                                                <STextField Class="ml-6" Type="number" NumberProps="(p)=>{ p.HideControl = true; p.Min = 1; p.Max = 10; }" @bind-Value="Model.FailedRetryCount" Label="@T("FailedRetryCount")" HideDetails="@("auto")" Dense Outlined />
                                            </MCol>
                                        }
                                    </MRow>
                                    <MRow NoGutters Class="mt-9">
                                        <MCol Cols="12">
                                            <STextarea @bind-Value="Model.Description" Label="@T("Description")" HideDetails="@("auto")" Outlined />
                                        </MCol>
                                    </MRow>
                                </AutoHeightContent>
                                <FooterContent>
                                    <div class="d-flex mt-9">
                                        @if (_isAdd)
                                        {
                                            <SButton Class="mr-4" Icon OnClick="PreviousStep">
                                                <SIcon Size="24" Color="emphasis2" Tooltip="@T("PreviousStep")">mdi-keyboard-backspace</SIcon>
                                            </SButton>
                                        }
                                        <MSpacer></MSpacer>
                                        <SButton BorderRadiusClass="btn-form" Class="ml-6" OnClick="()=> NextStep(context)">@T("NextStep")</SButton>
                                    </div>
                                </FooterContent>
                            </AutoHeight>
                        </MForm>
                    </MStepperContent>
                    <MStepperContent Step="3" Class="full-height pa-0">
                        @if (Model.JobType == JobTypes.Http)
                        {
                            <MForm Model=Model.HttpConfig EnableValidation @ref="httpForm" Class="full-height">
                                <AutoHeight Overflow>
                                <AutoHeightContent>
                                    <MRow>
                                        <MCol Md="6">
                                            <SSelect @bind-Value="Model.HttpConfig.HttpMethod" Label="@T("HttpMethod")" HideDetails="@("auto")" Attach="true" Dense Outlined Clearable Items="GetEnumMap<HttpMethods>()" ItemText="kv => T(kv.Key)" ItemValue="kv=>kv.Value">
                                                </SSelect>
                                        </MCol>
                                        <MCol Md="6">
                                                <MSwitch @bind-Value="Model.HttpConfig.IsAsync" TValue="bool" Inset Class="label my-auto flex-shrink-0 mt-1">
                                                    <LabelContent>
                                                        <span class="regular2--text body2">@T("AsyncMode")</span>
                                                        <DefaultTooltip Class="ml-3 my-auto" Tooltip="@T("AsyncModeTips")" />
                                                    </LabelContent>
                                                </MSwitch>
                                        </MCol>
                                    </MRow>
                                    <MRow>
                                        <MCol Md="6">
                                            <STextField @bind-Value="Model.HttpConfig.RequestUrl" Label="@T("RequestUrl")" HideDetails="@("auto")" Dense Outlined />
                                        </MCol>
                                    </MRow>
                                    <DefaultTitle>@T("RequestParameters")</DefaultTitle>
                                    <MRow>
                                        <MCol Md="6">
                                            <MButtonGroup Value="_httpParameterTypes.ToString()" Class="masa-buttongroup" Color="primary" Group Tile Dense Mandatory Borderless>
                                                @foreach (var (key, value) in GetEnumMap<HttpParameterTypes>())
                                                    {
                                                        <MButton OnClick="async() => await SwitchHttpParameterType(value)" Value="@value.ToString()" Ripple=false>
                                                            @T(key)
                                                        </MButton>
                                                    }
                                            </MButtonGroup>
                                        </MCol>
                                    </MRow>
                                        @if (_httpParameterTypes == HttpParameterTypes.Parameter)
                                        {
                                            <DynamicKeyValueComponent @bind-Value="Model.HttpConfig.HttpParameters">
                                            </DynamicKeyValueComponent>
                                        }
                                        else if (_httpParameterTypes == HttpParameterTypes.Headers)
                                        {
                                            <DynamicKeyValueComponent @bind-Value="Model.HttpConfig.HttpHeaders">
                                            </DynamicKeyValueComponent>
                                        }
                                        else
                                        {
                                            <MRow>
                                            <MCol Md="12">
                                            <STextarea @bind-Value="Model.HttpConfig.HttpBody" Label="@T("HttpBody")" HideDetails="@("auto")" Dense Outlined Rows="5">
                                                    </STextarea>
                                                    </MCol>
                                                    </MRow>
                                        }
                                        <MRow>
                                        <MCol Md="6">
                                            <SSelect @bind-Value="Model.HttpConfig.HttpVerifyType" Label="@T("HttpVerifyType")" HideDetails="@("auto")" Attach="true" Dense Outlined Clearable Items="GetEnumMap<HttpVerifyTypes>()" ItemText="kv => T(kv.Key)" ItemValue="kv=> kv.Value">
                                                </SSelect>
                                                </MCol>
                                                </MRow>
                                                <MRow>
                                        <MCol Md="12">
                                            <STextarea @bind-Value="Model.HttpConfig.VerifyContent" Label="@T("VerifyContent")" HideDetails="@("auto")" Dense Outlined Rows="5">
                                                </STextarea>
                                                </MCol>
                                                </MRow>
                                                <AuditedInfo Audited="(AuditedEntityDto)Model" />
                                            </AutoHeightContent>
                                            <FooterContent>
                                                <div class="d-flex mt-9 align-center">
                                                    <SButton Class="mr-4" Icon OnClick="PreviousStep">
                                            <SIcon Size="24" Color="emphasis2" Tooltip="@T("PreviousStep")">mdi-keyboard-backspace</SIcon>
                                                    </SButton>
                                            @if (Model.Id != Guid.Empty)
                                            {
                                                <span class="d-flex px-6" style="height: 12px">
                                                    <MDivider Vertical />
                                                </span>
                                                <SButton Icon Class="mr-4 align-self-center" OnClick="HandleDel">
                                                <SIcon Size="24" Color="error">mdi-delete</SIcon>
                                                </SButton>
                                            }
                                            <MSpacer></MSpacer>
                                            <SButton BorderRadiusClass="btn-form" Class="ml-6" OnClick="()=> Submit(context)">@T(_isAdd?"Submit":"Save")</SButton>
                                        </div>
                                    </FooterContent>
                                </AutoHeight>
                            </MForm>
                        }
                        else if (Model.JobType == JobTypes.JobApp)
                        {
                            <MForm Model=Model.JobAppConfig EnableValidation @ref="jobAppForm" Class="full-height">
                                <AutoHeight Overflow>
                                <AutoHeightContent>
                                    <MRow NoGutters Class="mt-1">
                                        <MCol Md="6">
                                            <SSelect Value="Model.JobAppConfig.JobAppIdentity" ValueExpression="()=> Model.JobAppConfig.JobAppIdentity" TValue="string" TItem="ProjectAppDto" TItemValue="string" ValueChanged="OnJobAppChanged" Label="@T("JobAppId")" HideDetails="@("auto")" Attach="true" Dense Outlined Clearable Items="_jobApp" ItemText="app => app.Name" ItemValue="app=>app.Identity">
                                                </SSelect>
                                                </MCol>
                                                </MRow>
                                                <MRow NoGutters Class="mt-9">
                                        <MCol Md="6">
                                            <STextField @bind-Value="Model.JobAppConfig.JobEntryAssembly" Placeholder="*.dll" Label="@T("JobEntryAssembly")" HideDetails="@("auto")" Dense Outlined>
                                            </STextField>
                                        </MCol>
                                    </MRow>
                                    <MRow NoGutters Class="mt-9">
                                        <MCol Md="6">
                                            <STextField @bind-Value="Model.JobAppConfig.JobEntryClassName" Placeholder="Namespace.Class" Label="@T("JobEntryClass")" HideDetails="@("auto")" Outlined>
                                            </STextField>
                                        </MCol>
                                    </MRow>
                                    <MRow NoGutters Class="mt-9">
                                        <MCol Md="12">
                                            <STextarea @bind-Value="Model.JobAppConfig.JobParams" Label="@T("OptionParams")" HideDetails="@("auto")" Dense Outlined>
                                            </STextarea>
                                        </MCol>
                                    </MRow>
                                    <DefaultTitle>@T("ResourceVersion")</DefaultTitle>
                                    <MRow NoGutters Class="mt-9">
                                        <MCol Md="6">
                                            <MButtonGroup Value="_resourceVersionType.ToString()" Class="rounded-lg elevation-tab" Group Tile Dense Mandatory Borderless>
                                                @foreach (var (key, value) in GetEnumMap<ResourceVersionTypes>())
                                                    {
                                                        <SButton OnClick="async() => await SwitchResourceVersionType(value)" Value="@value.ToString()" Ripple=false Class="rounded-lg" MinWidth=100>
                                                            @T(key)
                                                        </SButton>
                                                    }
                                                </MButtonGroup>
                                            </MCol>
                                            @if (_resourceVersionType == ResourceVersionTypes.SpecifiedVersion)
                                            {
                                                <MCol Md="6">
                                                    <SSelect @bind-Value="Model.JobAppConfig.Version" Label="@T("ResourceVersion")" HideDetails="@("auto")" Attach="true" Dense Outlined Clearable Items="_versionList" ItemText="version => version" ItemValue="version=> version">
                                                    </SSelect>
                                                    </MCol>
                                            }
                                        </MRow>
                                        <AuditedInfo Audited="(AuditedEntityDto)Model" />
                                    </AutoHeightContent>
                                    <FooterContent>
                                        <div class="d-flex mt-9 align-center">
                                            <SButton Class="mr-4" Icon OnClick="PreviousStep">
                                                <SIcon Size="24" Color="emphasis2" Tooltip="@T("PreviousStep")">mdi-keyboard-backspace</SIcon>
                                            </SButton>
                                            @if (Model.Id != Guid.Empty)
                                            {
                                                <span class="d-flex px-6" style="height: 12px">
                                                    <MDivider Vertical />
                                                </span>
                                                <SButton Icon Class="mr-4 align-self-center" OnClick="HandleDel">
                                                <SIcon Size="24" Color="error">mdi-delete</SIcon>
                                                </SButton>
                                            }
                                            <MSpacer></MSpacer>
                                            <SButton BorderRadiusClass="btn-form" Class="ml-6" OnClick="()=> Submit(context)">@T(_isAdd?"Submit":"Save")</SButton>
                                        </div>
                                    </FooterContent>
                                </AutoHeight>
                            </MForm>
                        }
                        else if (Model.JobType == JobTypes.DaprServiceInvocation)
                        {
                            <MForm Model=Model.DaprServiceInvocationConfig EnableValidation @ref="daprForm" Class="full-height">
                                <AutoHeight Overflow>
                                <AutoHeightContent>
                                    <MRow NoGutters Class="mt-1">
                                        <MCol Md="6">
                                            <SSelect Value="Model.DaprServiceInvocationConfig.DaprServiceIdentity" TValue="string" TItemValue="string" TItem="ProjectAppDto" ValueChanged="DaprServiceAppChange" Label="@T("DaprServiceAppId")" HideDetails="@("auto")" Attach="true" Dense Outlined Clearable Items="_serviceApp" ItemText="app => app.Name" ItemValue="app=>app.Identity">
                                                </SSelect>
                                                </MCol>
                                                </MRow>
                                                <MRow NoGutters Class="mt-9">
                                        <MCol Md="6">
                                            <STextField @bind-Value="Model.DaprServiceInvocationConfig.Namespace" Label="@T("Namespace")" HideDetails="@("auto")" Dense Outlined>
                                            </STextField>
                                        </MCol>
                                    </MRow>
                                    <MRow NoGutters Class="mt-9">
                                        <MCol Md="6">
                                            <STextField @bind-Value="Model.DaprServiceInvocationConfig.MethodName" Label="@T("MethodName")" HideDetails="@("auto")" Dense Outlined>
                                            </STextField>
                                        </MCol>
                                    </MRow>
                                    <MRow NoGutters Class="mt-9">
                                        <MCol Md="6">
                                            <SSelect @bind-Value="Model.DaprServiceInvocationConfig.HttpMethod" Label="@T("HttpMethod")" HideDetails="@("auto")" Attach="true" Dense Outlined Clearable Items="GetEnumMap<HttpMethods>()" ItemText="kv => T(kv.Key)" ItemValue="kv=>kv.Value">
                                                </SSelect>
                                                </MCol>
                                                </MRow>
                                                <MRow NoGutters Class="mt-9">
                                        <MCol Md="12">
                                            <STextarea @bind-Value="Model.DaprServiceInvocationConfig.Data" Label="@T("Data")" HideDetails="@("auto")" Dense Outlined Rows="10">
                                                </STextarea>
                                                </MCol>
                                                </MRow>
                                                <AuditedInfo Audited="(AuditedEntityDto)Model" />
                                            </AutoHeightContent>
                                            <FooterContent>
                                                <div class="d-flex mt-9" style="align-items: center;">
                                                    <SButton Class="mr-4" Icon OnClick="PreviousStep">
                                                        <SIcon Size="24" Color="emphasis2" Tooltip="@T("PreviousStep")">mdi-keyboard-backspace</SIcon>
                                                    </SButton>
                                            @if (Model.Id != Guid.Empty)
                                            {
                                                <span class="d-flex px-6 align-center">
                                                    <MDivider Vertical />
                                                </span>
                                                <SButton Icon Class="mr-4 align-self-center" OnClick="HandleDel">
                                                <SIcon Size="24" Color="error">mdi-delete</SIcon>
                                                </SButton>
                                            }
                                            <MSpacer></MSpacer>
                                            <SButton BorderRadiusClass="btn-form" Class="ml-6" OnClick="()=> Submit(context)">@T(_isAdd?"Submit":"Save")</SButton>
                                        </div>
                                    </FooterContent>
                                </AutoHeight>
                            </MForm>
                        }
                    </MStepperContent>
                </MStepperItems>
            </MStepper>
        </ChildContent>
    </SSimpleModal>
    <SSimpleModal @bind-Value="@_cronVisible" Title="@T("CronExpression")" SaveText="@T("Sure")" OnSave="() => SetCronExpression()">
        <PCron @bind-Value="@(_tempCron)">
        </PCron>
    </SSimpleModal>
</div>

<LogAlarmRuleUpsertModal @ref="_logUpsertModal" OnOk="HandleAlarmRuleUpsert" />